
project, uses(../data_output/termlimited_includenontermlimitstates.dta)

	use ../data_output/termlimited_includenontermlimitstates.dta, clear

	*** create treated flag

		*construct principal components based on sponsorship and cmt activity
		pca log_sponsored cmt_index
		predict pc, score
		sum pc
		replace pc = [pc - r(mean) ]/ r(sd) 	
		
		corr pc pc1 // correlates at .93

	egen treated = max(termlimited), by(CandId)

	*** remove control units from treated states
	drop if treated == 0 & (state != "NY" & state != "TX")

	gen control_state = state == "NY" | state == "TX"

	*** for each treated leg, create control unit from control states
	sort control_state CandId 

	keep CandId year control_state treated termlimited pc log_sponsored cmt_index permitted_terms terms_in_office

	keep if pc != .

	sort CandId year
	by CandId: gen num_obs = _N
	keep if control_state == 1 | num_obs==permitted_terms

	drop num_obs

	preserve
		keep if control_state == 1
		*** the control match will be based on the year and terms in office
		collapse (mean) pc log_sponsored cmt_index, by(year terms_in_office)
		rename pc pc1
		rename log_sponsored log_sponsored1
		rename cmt_index cmt_index1
		save ../data_output/tmp_match_control.dta, replace
	restore

	drop if control_state == 1
	rename pc pc0
	rename log_sponsored log_sponsored0
	rename cmt_index cmt_index0
	merge m:1 year terms_in_office using ../data_output/tmp_match_control.dta

	keep if _merge == 3
	drop _merge

	drop terms_in_office treated control_state permitted_terms

	reshape long pc log_sponsored cmt_index, i(CandId year) j(control)
	replace termlimited = 0 if control == 1

	rename CandId MatchId
	egen CandId = group(MatchId control)

	save ../data_output/match_analysis_data.dta, replace

	project, creates(../data_output/match_analysis_data.dta)
